package com.tender.service.impl;

import com.tender.constants.Constant;
import com.tender.service.LockService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tender.service.RedisService;

@Service
public class LockServiceImpl implements LockService {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RedisService redisService;

    @Override
    public void purchase() {
        RLock lock = redissonClient.getLock("lock");
        lock.lock();
        try {
            // do something
            String value = redisService.get(Constant.KEY);
            System.out.println("==============> 剩余库存：" + value);
            redisService.set(Constant.KEY, Integer.toString(Integer.parseInt(value) - 1));
            System.out.println("==============> 扣减库存成功");
        } finally {
            lock.unlock();
        }
    }
}
